package com.listener;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class AnayXML {

	public static Map<String,ActionMapping> anay(String  filepath){
		
		Map<String,ActionMapping> map=new HashMap<String, ActionMapping>();
		try {
			Document doc=new SAXReader().read(new File(filepath));
			Element el=doc.getRootElement();
			Iterator<Element> elist= el.elementIterator();
			String path="";
			while(elist.hasNext()){
				ActionMapping action=new ActionMapping();
				Element emap=elist.next();
				 path=emap.attributeValue("path");
				action.setPath(path);
				String type=emap.attributeValue("type");
				action.setType(type);
				String method=emap.attributeValue("method");
				action.setMethod(method);
				Iterator<Element> efor=emap.elementIterator("forward");
				Map<String,String> eformap=new HashMap<String, String>();

				while(efor.hasNext()){
				  Element ef=efor.next();
				  String name=ef.attributeValue("name");
				  String val=ef.getText();
				  eformap.put(name,val );
					
				}
				action.setForward(eformap);
				if(path.startsWith("/")){
					
					map.put(path.substring(1),action);

				}else{
					map.put(path, action);
					
				}
				
				
				
				
			}
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return map;
	}
		
		
		
		
	

}
